/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oim.fx.app;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.UIManager;

import com.oim.core.app.AppContext;
import com.oim.core.common.action.CallAction;
import com.oim.core.common.app.view.AddUserView;
import com.oim.core.common.app.view.ChatListView;
import com.oim.core.common.app.view.CreateGroupView;
import com.oim.core.common.app.view.FileDownloadView;
import com.oim.core.common.app.view.FileUploadView;
import com.oim.core.common.app.view.FindView;
import com.oim.core.common.app.view.GroupDataView;
import com.oim.core.common.app.view.GroupEditView;
import com.oim.core.common.app.view.JoinGroupView;
import com.oim.core.common.app.view.LoginView;
import com.oim.core.common.app.view.MainView;
import com.oim.core.common.app.view.NetSettingView;
import com.oim.core.common.app.view.RegisterView;
import com.oim.core.common.app.view.RemoteView;
import com.oim.core.common.app.view.ThemeView;
import com.oim.core.common.app.view.TrayView;
import com.oim.core.common.app.view.UpdatePasswordView;
import com.oim.core.common.app.view.UserDataEditView;
import com.oim.core.common.app.view.UserDataView;
import com.oim.core.common.app.view.VideoView;
import com.oim.core.common.config.ConfigManage;
import com.oim.core.common.config.data.Theme;
import com.oim.fx.common.box.ImageBox;
import com.oim.fx.view.AddUserViewImpl;
import com.oim.fx.view.ChatListViewImpl;
import com.oim.fx.view.CreateGroupViewImpl;
import com.oim.fx.view.FileDownloadViewImpl;
import com.oim.fx.view.FileUploadViewImpl;
import com.oim.fx.view.FindViewImpl;
import com.oim.fx.view.ForgetPasswordView;
import com.oim.fx.view.ForgetPasswordViewImpl;
import com.oim.fx.view.GroupDataViewImpl;
import com.oim.fx.view.GroupEditViewImpl;
import com.oim.fx.view.JoinGroupViewImpl;
import com.oim.fx.view.LoginViewImpl;
import com.oim.fx.view.MainViewImpl;
import com.oim.fx.view.RegisterViewImpl;
import com.oim.fx.view.UpdatePasswordViewImpl;
import com.oim.fx.view.UserChatHistoryView;
import com.oim.fx.view.UserChatHistoryViewImpl;
import com.oim.fx.view.UserDataEditViewImpl;
import com.oim.fx.view.UserDataViewImpl;
import com.oim.fx.view.UserHeadEditView;
import com.oim.fx.view.UserHeadEditViewImpl;
import com.oim.swing.UIBox;
import com.oim.swing.view.NetSettingViewImpl;
import com.oim.swing.view.RemoteViewImpl;
import com.oim.swing.view.ThemeViewImpl;
import com.oim.swing.view.TrayViewImpl;
import com.oim.swing.view.VideoViewImpl;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.stage.Stage;

/**
 * 2013-9-4 15:36:45
 * 
 * @author XiaHui
 */
public class AppStartup extends Application {

	AppContext appContext = new AppContext();

	public static void main(String[] args) {
		// OnlySystemUtil.addLibPath("dll");

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception ex) {
			java.util.logging.Logger.getLogger(AppStartup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		launch(args);

	}

	CallAction exitAction = new CallAction() {
		@Override
		public void execute() {
			Platform.exit();
			System.exit(0);
		}
	};

	@Override
	public void start(Stage primaryStage) throws Exception {
		javafx.scene.image.Image image = ImageBox.getImagePath("Resources/Images/Logo/logo_64.png");
		primaryStage.getIcons().clear();
		primaryStage.getIcons().add(image);
		Platform.setImplicitExit(false);
		appContext.getConnectThread().setAutoConnectCount(10);// 设置自动重试次数
		appContext.setExitAction(exitAction);
		initTheme();
		appContext.register(LoginView.class, LoginViewImpl.class);
		LoginView loginView = appContext.getSingleView(LoginView.class);
		loginView.setVisible(true);
		appContext.start();
		initView();
	}

	private void initView() {
		appContext.register(ChatListView.class, ChatListViewImpl.class);
		appContext.register(NetSettingView.class, NetSettingViewImpl.class);
		appContext.register(RegisterView.class, RegisterViewImpl.class);
		appContext.register(ForgetPasswordView.class, ForgetPasswordViewImpl.class);
		appContext.register(UserHeadEditView.class, UserHeadEditViewImpl.class);
		appContext.register(TrayView.class, TrayViewImpl.class);
		appContext.register(JoinGroupView.class, JoinGroupViewImpl.class);
		appContext.register(AddUserView.class, AddUserViewImpl.class);
		appContext.register(FindView.class, FindViewImpl.class);
		appContext.register(MainView.class, MainViewImpl.class);
		appContext.register(ThemeView.class, ThemeViewImpl.class);
		appContext.register(UpdatePasswordView.class, UpdatePasswordViewImpl.class);
		appContext.register(CreateGroupView.class, CreateGroupViewImpl.class);
		appContext.register(GroupDataView.class, GroupDataViewImpl.class);
		appContext.register(GroupEditView.class, GroupEditViewImpl.class);
		appContext.register(UserDataView.class, UserDataViewImpl.class);
		appContext.register(UserDataEditView.class, UserDataEditViewImpl.class);
		appContext.register(VideoView.class, VideoViewImpl.class);
		appContext.register(FileUploadView.class, FileUploadViewImpl.class);
		appContext.register(FileDownloadView.class, FileDownloadViewImpl.class);
		appContext.register(RemoteView.class, RemoteViewImpl.class);
		appContext.register(UserChatHistoryView.class, UserChatHistoryViewImpl.class);

		Task<Integer> task = new Task<Integer>() {
			@Override
			protected Integer call() throws Exception {
				int iterations = 0;
				Platform.runLater(new Runnable() {
					@Override
					public void run() {
						appContext.getSingleView(MainView.class);
						appContext.getSingleView(ChatListView.class);
						// appContext.getSingleView(RegisterView.class);
					}
				});

				appContext.getSingleView(TrayView.class);
				// appContext.getSingleView(NetSettingView.class);

				// appContext.getSingleView(ThemeView.class);
				// appContext.getSingleView(JoinGroupView.class);
				// appContext.getSingleView(AddUserView.class);
				// appContext.getSingleView(FindView.class);
				// appContext.getSingleView(MainView.class);
				appContext.setViewReady(true);
				return iterations;
			}
		};

		Thread th = new Thread(task);
		th.setDaemon(true);
		th.start();
	}

	/**
	 * 初始化主题图片
	 */
	private void initTheme() {
		Theme theme = (Theme) ConfigManage.get(Theme.config_file_path, Theme.class);

		Image imaeg = new ImageIcon(theme.getWindowBackgroundImage()).getImage();
		if (imaeg.getWidth(null) > 0 && imaeg.getHeight(null) > 0) {
			BufferedImage bi = new BufferedImage(imaeg.getWidth(null), imaeg.getHeight(null), BufferedImage.TYPE_INT_RGB);
			Graphics2D biContext = bi.createGraphics();
			biContext.drawImage(imaeg, 0, 0, null);
			// bi = OnlyImageUtil.applyGaussianFilter(bi, null,
			// theme.getGaussian());
			UIBox.put("key_window_background_image", bi);
			UIBox.put("key_window_background_image_path", theme.getWindowBackgroundImage());
			// UIBox.update();
		}
	}
}
